package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CateGoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CateGoryMapper cateGoryMapper;
//菜品分页查询
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);
        int count = dishMapper.selectCount(dish);
        int start,end;
        //计算开始下标
        start = (dishPageQueryDTO.getPage()-1) * dishPageQueryDTO.getPageSize() < 0 ? 0 :
                (dishPageQueryDTO.getPage()-1) * dishPageQueryDTO.getPageSize();
        System.out.println((dishPageQueryDTO.getPage()-1) * dishPageQueryDTO.getPageSize());
        //计算需要几条数据
        end = dishPageQueryDTO.getPage() * dishPageQueryDTO.getPageSize() <= count ?
                dishPageQueryDTO.getPageSize() :
                (count - (dishPageQueryDTO.getPage()-1) * dishPageQueryDTO.getPageSize() < 0 ? count :
                        count - (dishPageQueryDTO.getPage()-1) * dishPageQueryDTO.getPageSize());
        List<Dish> dish2 = dishMapper.select(dish,start,end);
        List<DishDTO> records = new ArrayList<>();
        for (Dish dish3 : dish2) { // 在这里添加了Dish作为迭代变量，并指定了records作为要遍历的列表
            DishDTO dishDTO = new DishDTO();
            BeanUtils.copyProperties(dish3,dishDTO);

            records.add(dishDTO); // 将创建的DishDTO对象添加到列表中
        }
        for (DishDTO dishDTO : records){
            dishDTO.setCategoryName(cateGoryMapper.selectByid(dishDTO.getCategoryId()));
        }

        return new PageResult(count,records);
    }
//菜品启停
    @Override
    public void sos(Integer status,Long id) {
        DishDTO dishDTO = new DishDTO();
        dishDTO.setId(id);
        dishDTO.setStatus(status);
        dishDTO.setUpdateTime(LocalDateTime.now());
        dishDTO.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.update(dishDTO);
    }
//新增菜品
    @Override
    public void addDish(DishDTO dishDTO) {
        dishDTO.setStatus(1);
        dishDTO.setUpdateTime(LocalDateTime.now());
        dishDTO.setCreateTime(LocalDateTime.now());
        dishDTO.setCreateUser(BaseContext.getCurrentId());
        dishDTO.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.add(dishDTO);
        Long id = dishMapper.selectByName(dishDTO.getName());
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        for (DishFlavor d: dishFlavors) {
            d.setDishId(id);
            dishFlavorMapper.add(d);
        }

    }
//根据id查询菜品
    @Override
    public DishDTO getDish(Long id) {
        DishDTO dishDTO = dishMapper.selectById(id);
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectById(id);
        dishDTO.setFlavors(dishFlavor);
        return dishDTO;
    }
//删除商品
    @Override
    public void deleteDish(String ids) {
        String[] idss = ids.split(",");
        for (String i: idss) {
            long x = Long.valueOf(i);
            dishMapper.deleDish(x);
            dishFlavorMapper.deleDish(x);
        }

    }
//修改商品
    @Override
    public void updateDish(DishDTO dishDTO) {
        deleteDish(String.valueOf(dishDTO.getId()));//直接删除再添加 实现修改功能
        addDish(dishDTO);
    }
//根据分类查询菜品
    @Override
    public List<DishDTO> list(Long categoryId) {
        return dishMapper.selectByCategory(categoryId);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
